package com.baseweb.webadmin.core.model.dao.impl;

import com.baseweb.webadmin.core.model.bvo.UserRoleBvo;
import com.baseweb.webadmin.core.model.dao.ContentDao;
import com.baseweb.webadmin.core.model.mapper.*;
import com.baseweb.webadmin.core.model.pojo.*;
import org.apache.commons.lang.StringUtils;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

public class ContentDaoImpl extends SqlSessionDaoSupport implements ContentDao {

    @Autowired
    UserMapper userMapper;

    @Autowired
    ByUserFeedbackMapper byUserFeedbackMapper;

    @Autowired
    ByLevelMapper byLevelMapper;

    @Autowired
    ByDictMapper byDictMapper;

    @Autowired
    ByChannelMapper byChannelMapper;

    @Autowired
    ByNoticeMapper byNoticeMapper;

    @Autowired
    ByAdMapper byAdMapper;

    @Autowired
    ByArticleMapper byArticleMapper;

    @Autowired
    ByUserCommentMapper byUserCommentMapper;

    @Autowired
    ByChannelKeywordMapper byChannelKeywordMapper;

    @Override
    public User selectUserByUserName(String username) {
        return this.getSqlSession().selectOne("com.baseweb.webadmin.core.model.dao.UserDao.selectUserByUserName", username);
    }

    @Override
    public List<User> selectUsers(String username) {
        return this.getSqlSession().selectOne("com.baseweb.webadmin.core.model.dao.UserDao.selectUsers", username);
    }

    @Override
    public List<UserRoleBvo> selectUsersByPage(Map map) {
//        if (pageNo < 1) {
//            pageNo = 1;
//        }
//
//        if (pageSize < 1) {
//            pageSize = 20;
//        }
//
//        int startCount = (pageNo - 1) * pageSize;
//        map.put("startCount", startCount);
//        map.put("pageSize", pageSize);

        return getSqlSession().selectList("com.baseweb.webadmin.core.model.dao.UserDao.selectUsersByPage", map);
    }

    @Override
    public Integer selectUsersByNameRows(Map map) {
        return (Integer) getSqlSession().selectOne("com.baseweb.webadmin.core.model.dao.UserDao.selectUsersByNameRows", map);
    }

    /**
     * 根据邮箱在user表中判断该邮箱
     * 是否已经被使用
     *
     * @param email
     * @return
     */
    @Override
    public List<User> selectUserByEmail(String email) {
        return this.getSqlSession().selectList("com.baseweb.webadmin.core.model.dao.UserDao.selectUserByEmail", email);
    }

    @Override
    public List<User> selectUserListByPage(Integer id, String username, Integer start, Integer offset) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        if(StringUtils.isNotBlank(username)){
            criteria.andUsernameEqualTo(username);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return userMapper.selectByExample(example);
    }

//    @Override
//    public Integer selectUserListCount(Integer id, String username) {
//        UserExample example = new UserExample();
//        UserExample.Criteria criteria = example.createCriteria();
//        if(id != null){
//            criteria.andIdEqualTo(id);
//        }
//        if(StringUtils.isNotBlank(username)){
//            criteria.andUsernameEqualTo(username);
//        }
//        return userMapper.countByExample(example);
//    }


    @Override
    public Integer insertFeedback(ByUserFeedback byUserFeedback) {
        if (byUserFeedback.getId() !=null && byUserFeedback.getId() > 0){
            return byUserFeedbackMapper.updateByPrimaryKey(byUserFeedback);
        }else {
            return byUserFeedbackMapper.insert(byUserFeedback);
        }
    }

    @Override
    public ByUserFeedback findFeedback(Integer id) {
        return byUserFeedbackMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByUserFeedback> selectFeedbackListByPage(Integer userId, Integer start, Integer offset) {
        ByUserFeedbackExample example = new ByUserFeedbackExample();
        ByUserFeedbackExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserFeedbackMapper.selectByExample(example);
    }

    @Override
    public Integer selectFeedbackListCount(Integer userId) {
        ByUserFeedbackExample example = new ByUserFeedbackExample();
        ByUserFeedbackExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        return byUserFeedbackMapper.countByExample(example);
    }
    @Override
    public List<ByUserFeedback> selectFeedbackListByPage1(ByUserFeedback feedback, Integer start, Integer offset) {
        ByUserFeedbackExample example = new ByUserFeedbackExample();
        ByUserFeedbackExample.Criteria criteria = example.createCriteria();
        if(feedback.getUserId() != null){
            criteria.andUserIdEqualTo(feedback.getUserId());
        }
        if(feedback.getContact() != null){
            criteria.andContactLike("%"+feedback.getContact()+"%");
        }
        if(feedback.getStatus() != null){
            criteria.andStatusEqualTo(feedback.getStatus());
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserFeedbackMapper.selectByExample(example);
    }

    @Override
    public Integer selectFeedbackListCount1(ByUserFeedback feedback) {
        ByUserFeedbackExample example = new ByUserFeedbackExample();
        ByUserFeedbackExample.Criteria criteria = example.createCriteria();
        if(feedback.getUserId() != null){
            criteria.andUserIdEqualTo(feedback.getUserId());
        }
        if(feedback.getContact() != null){
            criteria.andContactLike("%"+feedback.getContact()+"%");
        }
        if(feedback.getStatus() != null){
            criteria.andStatusEqualTo(feedback.getStatus());
        }
        return byUserFeedbackMapper.countByExample(example);
    }
    @Override
    public List<ByLevel> selectLevelListByPage(Integer start, Integer offset) {
        ByLevelExample example = new ByLevelExample();
        ByLevelExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byLevelMapper.selectByExample(example);
    }

    @Override
    public Integer selectLevelListCount() {
        ByLevelExample example = new ByLevelExample();
        ByLevelExample.Criteria criteria = example.createCriteria();
        return byLevelMapper.countByExample(example);
    }

    @Override
    public List<ByDict> selectDictListByPage(Integer dictType, Integer start, Integer offset) {
        ByDictExample example = new ByDictExample();
        ByDictExample.Criteria criteria = example.createCriteria();
        if(dictType != null){
            criteria.andDictTypeEqualTo(dictType);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byDictMapper.selectByExample(example);
    }

    @Override
    public Integer selectDictListCount(Integer dictType) {
        ByDictExample example = new ByDictExample();
        ByDictExample.Criteria criteria = example.createCriteria();
        if(dictType != null){
            criteria.andDictTypeEqualTo(dictType);
        }
        return byDictMapper.countByExample(example);
    }

    @Override
    public Integer insertChannel(ByChannel byChannel) {
        if (byChannel.getId() !=null && byChannel.getId() > 0){
            return byChannelMapper.updateByPrimaryKey(byChannel);
        }else {
            return byChannelMapper.insert(byChannel);
        }
    }

    @Override
    public ByChannel findChannel(Integer id) {
        return byChannelMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByChannel> selectChannelListByPage(Integer positionId, Integer parentId, Integer status, Integer start, Integer offset) {
        ByChannelExample example = new ByChannelExample();
        ByChannelExample.Criteria criteria = example.createCriteria();
        if(positionId != null){
            criteria.andPositionIdEqualTo(positionId);
        }
        if(parentId != null){
            criteria.andParentIdEqualTo(parentId);
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
        example.setOrderByClause(String.format("position_id asc,sort desc limit %s,%s", start, offset));
        return byChannelMapper.selectByExample(example);
    }
    @Override
    public List<ByChannel> selectChannelListByPage1(ByChannel channel, Integer start, Integer offset) {
        return byChannelMapper.selectByChannelListPage(channel,start,offset);
    }
    @Override
    public List<ByChannel> selectChannelListLevel2() {
        ByChannelExample example = new ByChannelExample();
        ByChannelExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(1);
        criteria.andLevelEqualTo(2);
        example.setOrderByClause("sort asc");
        return byChannelMapper.selectByExample(example);
    }
    @Override
    public List<ByAd> findAllByAdByPrarentId(int parentId, int limit,String city) {
        ByAdExample example = new ByAdExample();
        ByAdExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(1);
        criteria.andParentIdEqualTo(parentId);
        if(city != null)
            criteria.andCityEqualTo(city);
        //审核通过
        criteria.andIsCheckEqualTo(1);
        example.setOrderByClause(String.format("sort desc limit %s", limit));
        return byAdMapper.selectByExample(example);
    }
    @Override
    public Integer selectChannelListCount(Integer positionId, Integer parentId, Integer status) {
        ByChannelExample example = new ByChannelExample();
        ByChannelExample.Criteria criteria = example.createCriteria();
        if(positionId != null){
            criteria.andPositionIdEqualTo(positionId);
        }
        if(parentId != null){
            criteria.andParentIdEqualTo(parentId);
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
        return byChannelMapper.countByExample(example);
    }
    @Override
    public Integer selectChannelListCount1(ByChannel channel) {
        channel.setFindFlag(2);
        return byChannelMapper.selectByChannelCountPage(channel);
    }

    @Override
    public Integer insertNotice(ByNotice byNotice) {
        if (byNotice.getId() !=null && byNotice.getId() > 0){
            return byNoticeMapper.updateByPrimaryKey(byNotice);
        }else {
            return byNoticeMapper.insert(byNotice);
        }
    }

    @Override
    public List<ByChannel> selectAllSubChannelList() {
        ByChannelExample example = new ByChannelExample();
        ByChannelExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdGreaterThan(0);
        return byChannelMapper.selectByExample(example);
    }

    @Override
    public ByNotice findNotice(Integer id) {
        return byNoticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByNotice> selectNoticeListByPage(Integer userId, Integer noticeType, Integer start, Integer offset) {
        ByNoticeExample example = new ByNoticeExample();
        ByNoticeExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(noticeType != null){
            criteria.andNoticeTypeEqualTo(noticeType);
        }
        example.setOrderByClause(String.format("sort desc limit %s,%s", start, offset));
        return byNoticeMapper.selectByExample(example);
    }

    @Override
    public Integer selectNoticeListCount(Integer userId, Integer noticeType) {
        ByNoticeExample example = new ByNoticeExample();
        ByNoticeExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(noticeType != null){
            criteria.andNoticeTypeEqualTo(noticeType);
        }
        return byNoticeMapper.countByExample(example);
    }

    @Override
    public Integer insertAd(ByAd byAd) {
        if (byAd.getId() !=null && byAd.getId() > 0){
            return byAdMapper.updateByPrimaryKey(byAd);
        }else {
            return byAdMapper.insert(byAd);
        }
    }

    @Override
    public ByAd findAd(Integer id) {
        return byAdMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer deleteAd(Integer id) {
        return byAdMapper.deleteByPrimaryKey(id);
    }
    @Override
    public Integer deleteChannel(Integer id) {
        return byChannelMapper.deleteByPrimaryKey(id);
    }
    @Override
    public Integer deleteChannelKeyWord(Integer id) {
        return byChannelKeywordMapper.deleteByPrimaryKey(id);
    }
    @Override
    public List<ByAd> selectAdListByPage(Integer positionId, Integer parentId, Integer status, Integer start, Integer offset) {
        ByAdExample example = new ByAdExample();
        ByAdExample.Criteria criteria = example.createCriteria();
        if(positionId != null){
            criteria.andPositionIdEqualTo(positionId);
        }
        if(parentId != null){
            criteria.andParentIdEqualTo(parentId);
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
        example.setOrderByClause(String.format("position_id asc, sort desc limit %s,%s", start, offset));
        return byAdMapper.selectByExample(example);
    }

    @Override
    public Integer selectAdListCount(Integer positionId, Integer parentId, Integer status) {
        ByAdExample example = new ByAdExample();
        ByAdExample.Criteria criteria = example.createCriteria();
        if(positionId != null){
            criteria.andPositionIdEqualTo(positionId);
        }
        if(parentId != null){
            criteria.andParentIdEqualTo(parentId);
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
        return byAdMapper.countByExample(example);
    }
    @Override
    public List<ByAd> selectAdListByPage1(ByAd byAd, Integer start, Integer offset) {
        ByAdExample example = new ByAdExample();
        ByAdExample.Criteria criteria = example.createCriteria();
        if(byAd.getPositionId() != null){
            criteria.andPositionIdEqualTo(byAd.getPositionId());
        }
        if(byAd.getParentId() != null){
            criteria.andParentIdEqualTo(byAd.getParentId());
        }
        if (byAd.getStatus() != null){
            criteria.andStatusEqualTo(byAd.getStatus());
        }
        if (byAd.getName() != null){
            criteria.andNameLike(byAd.getName() );
        }
        if (byAd.getProvince() != null){
            criteria.andProvinceEqualTo(byAd.getProvince() );
        }
        if (byAd.getCity() != null){
            criteria.andCityEqualTo(byAd.getCity() );
        }
        if (byAd.getDistrict() != null){
            criteria.andDistrictEqualTo(byAd.getDistrict() );
        }
        if (byAd.getIsCheck() != null){
            criteria.andIsCheckEqualTo(byAd.getIsCheck() );
        }
        example.setOrderByClause(String.format("sort desc limit %s,%s", start, offset));
        return byAdMapper.selectByExample(example);
    }
    @Override
    public List<ByAd> findAllByParentId(int parentId,int limit,String city) {
        return byAdMapper.findAllByParentId(parentId, limit, city);
    }
    @Override
    public List<ByAd> findAllByParentId(int parentId) {
        return byAdMapper.findByAdParentId(parentId);
    }
    @Override
    public List<ByChannel> findChannelByParentId(Integer parentId) {
        return byChannelMapper.findChannelByParentId(parentId);
    }
    @Override
    public List<ByChannelKeyword> findChannelKeywordByParentId(Integer parentId) {
        return byChannelKeywordMapper.findChannelKeywordByParentId(parentId);
    }
    @Override
    public Integer selectAdListCount1(ByAd byAd) {
        ByAdExample example = new ByAdExample();
        ByAdExample.Criteria criteria = example.createCriteria();
        if(byAd.getPositionId() != null){
            criteria.andPositionIdEqualTo(byAd.getPositionId());
        }
        if(byAd.getParentId() != null){
            criteria.andParentIdEqualTo(byAd.getParentId());
        }
        if (byAd.getStatus() != null){
            criteria.andStatusEqualTo(byAd.getStatus());
        }
        if (byAd.getName() != null){
            criteria.andNameLike(byAd.getName() );
        }
        if (byAd.getProvince() != null){
            criteria.andProvinceEqualTo(byAd.getProvince() );
        }
        if (byAd.getCity() != null){
            criteria.andCityEqualTo(byAd.getCity() );
        }
        if (byAd.getDistrict() != null){
            criteria.andDistrictEqualTo(byAd.getDistrict() );
        }
        if (byAd.getIsCheck() != null){
            criteria.andIsCheckEqualTo(byAd.getIsCheck() );
        }
        return byAdMapper.countByExample(example);
    }
    @Override
    public List<ByArticle> selectArticleListByPage(Integer userId, Integer positionId, Integer articleType, Integer start, Integer offset) {
        ByArticleExample example = new ByArticleExample();
        ByArticleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(positionId != null){
            criteria.andPositionIdEqualTo(positionId);
        }
        if(articleType != null){
            criteria.andArticleTypeEqualTo(articleType);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byArticleMapper.selectByExample(example);
    }

    @Override
    public Integer selectArticleListCount(Integer userId, Integer positionId, Integer articleType) {
        ByArticleExample example = new ByArticleExample();
        ByArticleExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(positionId != null){
            criteria.andPositionIdEqualTo(positionId);
        }
        if(articleType != null){
            criteria.andArticleTypeEqualTo(articleType);
        }
        return byArticleMapper.countByExample(example);
    }

    @Override
    public List<ByUserComment> selectCommentListByPage(Integer userId, String tableName, String tableId, Integer start, Integer offset) {
        ByUserCommentExample example = new ByUserCommentExample();
        ByUserCommentExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(tableName != null){
            criteria.andTableNameEqualTo(tableName);
        }
        if(tableId != null){
            criteria.andTableIdEqualTo(tableId);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserCommentMapper.selectByExample(example);
    }

    @Override
    public Integer selectCommentListCount(Integer userId, String tableName, String tableId) {
        ByUserCommentExample example = new ByUserCommentExample();
        ByUserCommentExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(tableName != null){
            criteria.andTableNameEqualTo(tableName);
        }
        if(tableId != null){
            criteria.andTableIdEqualTo(tableId);
        }
        return byUserCommentMapper.countByExample(example);
    }

    @Override
    public Integer insertLevel(ByLevel byLevel) {
        if (byLevel.getId() !=null && byLevel.getId() > 0){
            return byLevelMapper.updateByPrimaryKey(byLevel);
        }else {
            return byLevelMapper.insert(byLevel);
        }
    }

    @Override
    public ByLevel findLevel(Integer id) {
        return byLevelMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insertDict(ByDict byDict) {
        if (byDict.getId() !=null && byDict.getId() > 0){
            return byDictMapper.updateByPrimaryKey(byDict);
        }else {
            return byDictMapper.insert(byDict);
        }
    }

    @Override
    public ByDict findDict(Integer id) {
        return byDictMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insertArticle(ByArticle byArticle) {
        if (byArticle.getId() !=null && byArticle.getId() > 0){
            return byArticleMapper.updateByPrimaryKey(byArticle);
        }else {
            return byArticleMapper.insert(byArticle);
        }
    }

    @Override
    public ByArticle findArticle(Integer id) {
        return byArticleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer insertComment(ByUserComment byUserComment) {
        if (byUserComment.getId() !=null && byUserComment.getId() > 0){
            return byUserCommentMapper.updateByPrimaryKey(byUserComment);
        }else {
            return byUserCommentMapper.insert(byUserComment);
        }
    }

    @Override
    public ByUserComment findComment(Integer id) {
        return byUserCommentMapper.selectByPrimaryKey(id);
    }


    @Override
    public Integer insertKeyWord(ByChannelKeyword byChannelKeyword) {
        if (byChannelKeyword.getId() !=null && byChannelKeyword.getId() > 0){
            return byChannelKeywordMapper.updateByPrimaryKey(byChannelKeyword);
        }else {
            return byChannelKeywordMapper.insert(byChannelKeyword);
        }
    }

    @Override
    public ByChannelKeyword findKeyWord(Integer id) {
        return byChannelKeywordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByChannelKeyword> findKeyWordList(List<Integer> ids) {
        ByChannelKeywordExample example = new ByChannelKeywordExample();
        ByChannelKeywordExample.Criteria criteria = example.createCriteria();
        if(ids != null){
            criteria.andIdIn(ids);
        }else{
            return null;
        }
        return byChannelKeywordMapper.selectByExample(example);
    }

    @Override
    public List<ByChannelKeyword> selectKeyWordListByPage(Integer positionId, Integer parentId, String name, Integer status, Integer start, Integer offset) {
        ByChannelKeywordExample example = new ByChannelKeywordExample();
        ByChannelKeywordExample.Criteria criteria = example.createCriteria();
        if(positionId != null){
            criteria.andPositionIdEqualTo(positionId);
        }
        if(parentId != null){
            criteria.andParentIdEqualTo(parentId);
        }
        if(StringUtils.isNotBlank(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        example.setOrderByClause(String.format("parent_id asc, sort desc limit %s,%s", start, offset));
        return byChannelKeywordMapper.selectByExample(example);
    }

    @Override
    public List<ByChannelKeyword> selectKeyWordAllList(){
        ByChannelKeywordExample example = new ByChannelKeywordExample();
        ByChannelKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(1);
        return byChannelKeywordMapper.selectByExample(example);
    }

    @Override
    public Integer selectKeyWordListCount(Integer positionId, Integer parentId, String name, Integer status) {
        ByChannelKeywordExample example = new ByChannelKeywordExample();
        ByChannelKeywordExample.Criteria criteria = example.createCriteria();
        if(positionId != null){
            criteria.andPositionIdEqualTo(positionId);
        }
        if(parentId != null){
            criteria.andParentIdEqualTo(parentId);
        }
        if(StringUtils.isNotBlank(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        return byChannelKeywordMapper.countByExample(example);
    }

    //查询全部ByChannel
    @Override
    public List<ByChannel> findAllByChannelByLeftList(){
        return byChannelMapper.findAllByChannelByLeftList();
    }
    @Override
    public List<ByChannel> findAllByChannelKeyWorkByLeftList(){
        return byChannelMapper.findAllByChannelKeyWorkByLeftList();
    }

    //查询全部ByChannelKeyword
    @Override
    public List<ByChannelKeyword> findAllByChannelKeywordByLeftList(){
        return byChannelKeywordMapper.findAllByChannelKeywordByLeftList();
    }

    //根据positionId查询全部ByChannel
    @Override
    public List<ByChannel> findAllByChannelByPositionId(int positionId){
        return byChannelMapper.findAllByChannelByPositionId(positionId);
    }

    //根据positionId查询全部ByAd
    @Override
    public List<ByAd> findAllByAdByPositionId(int positionId,int limit,String city){
        return byAdMapper.findAllByPositionId(positionId,limit,city);
    }
}
